<include file="Public:header" />
<div class="container">
    <div class="row">
        <ol class="breadcrumb">
            <li><a href="{:U('topic/index')}">话题</a>
            </li>
            <li class="active">创建话题</li>
        </ol>
        <div class="col-md-9">
            <div class="panel">
                <div class="panel-heading">
                    <h3>创建话题</h3>
                </div>
                <div class="panel-body">
                    <form id="talk-add" action="" method="post" class="form-horizontal">
                        <fieldset>
                            <div class="form-group">
                                <div class="col-md-4">
                                    <select name="cid" id="cid" class="form-control">
                                        <option value="">选择分类</option>
                                        <volist name="cate_lists" id="vo">
                                            <option value="{$vo.id}" <if condition="$cid eq $vo['id']">selected="selected"</if> >{$vo.name}</option>
                                        </volist>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-8">
                                    <input type="text" id="subject" name="subject" class="form-control" value="" placeholder="标题">
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-6">
                                    <script type="text/plain" id="editor" style="width:660px;height:350px;"></script>
                                </div>
                            </div>
                            <div class="form-group">
                                <notempty name="cid">
                                    <input type="hidden" name="cid" value="{$cid}" >
                                </notempty>
                                <div class="col-md-6">
                                    <button type="submit" class="btn btn-primary" id="create">创建</button>
                                </div>
                            </div>
                        </fieldset>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<include file="Public:footer" />
<link href="__PUBLIC__/js/editor/themes/default/css/umeditor.css" type="text/css" rel="stylesheet">
<script src="__PUBLIC__/js/editor/umeditor.config.js"></script>
<script src="__PUBLIC__/js/editor/umeditor.min.js"></script>
<script src="__PUBLIC__/js/editor/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript">
$(function() {
    var um = UM.getEditor('editor', {
        toolbar: ['bold italic underline insertorderedlist insertunorderedlist  image '],
        UEDITOR_HOME_URL: "__PUBLIC__/js/editor/",
        imageUrl: "{:U('topic/upload')}",
        imagePath: "__PUBLIC__/upload/attach/",

        autoClearinitialContent: true,
        wordCount: false,
        elementPathEnabled: false,
        autoFloatEnabled: false,

        textarea: 'content'
    });

    $('#talk-add').on('submit', function(e) {
        e.preventDefault();
        var cid = $('#cid').val();
        var subject = $.trim($('#subject').val());
        var content = UM.getEditor('editor').getPlainTxt();
        if (cid == '') {
            bootbox.alert('分类不能为空');
            return false;
        }
        if (subject == '') {
            bootbox.alert('标题不能为空');
            return false;
        }
        if (content == false) {
            bootbox.alert('内容不能为空');
            return false;
        }               
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "{:U('topic/add')}",
            data: $("#talk-add").serialize(),
            success: function(data){
                if (data.status == 0) {
                    bootbox.alert(data.info);
                    return  false;
                } else {
                    window.location.href = data.url;
                }

            }
        });
    })
});
</script>
</body>
</html>